#!/usr/bin/perl

###############################################################################
#  InstalllationCheck
#
#  Copyright (c) 2006 onOne Software.
#  All Rights Reserved
#  
#  This module performs install checks
#
###############################################################################

use Cwd;
use File::Basename;

push @INC, "$ARGV[0]/Contents/Resources";
my $RES = "$ARGV[0]/Contents/Resources";
my $INST_TOOL = "$RES/installer_tool.app/Contents/MacOS/installer_tool";

#for updaters
my $GETPLISTENTRY    = "$RES/GetPListEntry.py";
my $VERSION_COMPARE  = "$RES/VersionCompare.py";

require "script_vars.pl";
require "SharedScript.pl";

exit main();


###############################################################################
#
###############################################################################
sub GetPListEntry #(inputPlist, key)
{
	my $key        = pop(@_);
	my $inputPlist = pop(@_);

	my $cmd = "\"$GETPLISTENTRY\" --input \"$inputPlist\" --key \"$key\" |";

	open CMD_RESULT, $cmd or die "ERROR: Couldn't open GetPListEntry for key $key!\n";
	my $entry = <CMD_RESULT>;
	close CMD_RESULT, $cmd or die "ERROR: Couldn't close GetPListEntry for key $key!\n";

	return $entry;
}

###############################################################################
#
###############################################################################
sub VersionCompare #(v1, v2)
{
	my $v2 = pop(@_);
	my $v1 = pop(@_);

	$cmd = "\"$VERSION_COMPARE\" --in1 \"$v1\" --in2 \"$v2\" |";       
	open CMD_RESULT, $cmd or die "ERROR: Couldn't open VersionCompare pipe!\n";
	$result = <CMD_RESULT>;
	close CMD_RESULT, $cmd or die "ERROR: Couldn't close VersionCompare pipe!\n";

	return $result;
}

###############################################################################
#
###############################################################################
sub BundleIsInstalled #(v1)
{
	my $v1 = pop(@_);

	$cmd = "pkgutil --pkgs | grep \"$v1\" |";       
	open CMD_RESULT, $cmd or die "ERROR: Couldn't open BundleIsInstalled pipe!\n";
	$result = <CMD_RESULT>;
	close CMD_RESULT, $cmd or die "ERROR: Couldn't close BundleIsInstalled pipe!\n";

	return $result;
}

###############################################################################
#
###############################################################################
sub VersionForBundle #(v1)
{
	my $v1 = pop(@_);

	$cmd = "pkgutil --pkg-info \"$v1\" | grep \"version: \" | sed 's/version: //' |";       
	open CMD_RESULT, $cmd or die "ERROR: Couldn't open VersionForBundle pipe!\n";
	$result = <CMD_RESULT>;
	close CMD_RESULT, $cmd or die "ERROR: Couldn't close VersionForBundle pipe!\n";

	return $result;
}

###############################################################################
#
# FindProductInstalls()
#
# For use with Free installers
# Find any installations on the machine, and if so do not install
# the Free version
#
###############################################################################
sub FindProductInstalls
{
	print "INSTALLCHECK::FindProductInstalls>\n";
	
	my $application = "";
	@applications = `ls /Applications`;
	
	foreach $application (@applications)
	{
		chomp($application);
	
		foreach $fullProd (@PRODUCT_NOT_INSTALLED)
		{
			
			if ($fullProd eq $application)
			{
				print "INSTALLCHECK::FindProductInstalls found other product: $application\n";
				return 1;
			}
		}
		
	}
	
	print "INSTALLCHECK::FindProductInstalls other Version not found\n";
	
	return 0;
	
}


###############################################################################
#
# FindUniquePSPluginDirs()
#
# Find all the unique Photoshop plug-in directories installed on this
# machine. Adobe places a file in the Preferences directory of each
# user who installs (or runs) Photoshop. We look for all these
# files, read them and compile a list of unique plug-in directories.
# This list is returned when the function completes.
#
###############################################################################
sub FindUniquePSPluginDirs
{
	print "INSTALLCHECK::FindUniquePSPluginDirs>\n";

	my $user = "";
	my $path = "";
	my $ps_path = "";
	my @pluginDirs;

	# The following list of users may contain
	# bogus users, such as "Shared", or "Deleted Users".
	# This is OK, since the Adobe file we are looking for
	#in such cases will not be found.
	@users = `ls /Users`;
	foreach $user (@users)
	{
		print "INSTALLCHECK::FindUniquePSPluginDirs> Checking user: $user\n";
		chomp($user);
		foreach $ps (@ALL_SUPPORTED)
		{
			$path = "/Users/$user/Library/Preferences/$ps Paths";
			print "INSTALLCHECK::FindUniquePSPluginDirs> Looking for path file: $path\n";
			if (-f "$path")
			{
				print "INSTALLCHECK::FindUniquePSPluginDirs> Found path file: $path\n";

				$ps_path = `"$INST_TOOL" readPhotoshopPath "$path"`;
				chop($ps_path);

				print "INSTALLCHECK::FindUniquePSPluginDirs> Extracted path: $ps_path\n";

				if (-d $ps_path)
				{
					if (join(" ", @pluginDirs) !~ "$ps_path")
					{
						print "INSTALLCHECK::FindUniquePSPluginDirs> Found $ps_path\n";
						push(@pluginDirs, "$ps_path");
					}
				}
			}
			else
			{
				$ps_path = "/Applications/$ps";
				print "INSTALLCHECK::FindUniquePSPluginDirs> Path file not found, looking for $ps_path\n";
				if (-d $ps_path)
				{
					if (join(" ", @pluginDirs) !~ "$ps_path")
					{
						print "INSTALLCHECK::FindUniquePSPluginDirs> Found $ps_path\n";
						push(@pluginDirs, "$ps_path");
					}
				}
				else
				{
					print "INSTALLCHECK::FindUniquePSPluginDirs> Still not found\n";
				}
			}
		}
	}

	my $count = scalar(@pluginDirs);
	print "INSTALLCHECK::FindUniquePSPluginDirs> Found $count places to install\n";
	return @pluginDirs;
}

###############################################################################
#
###############################################################################
sub main
{
	print "INSTALLCHECK::main>\n";

	my $cmd     = "";

	$hostRunning = do "IsHostAppRunning.pl";
	if($hostRunning != 0)
	{
		return $hostRunning;
	}

	if ( $SHOW_DEACTIVATE_WARNING == 1 )
	{
		my $reg_result = `"$INST_TOOL" "deactivateAlert" "none"`;
		chomp($reg_result);
		
		if ($reg_result =~ /no/)
		{
			print "INSTALLCHECK::main> result: $reg_result\n";
			
			# user hit the cancel button
			# cancel install
			return (3 << 5) + 17;
		}
	}

	if($UPDATE_INSTALL == 1)
	{
		# first see if there is a package with the proper version number
		foreach $rcpt (@REQUIRED_RECEIPTS)
		{
			$rcptPkg    = "/Library/Receipts/$rcpt";
			$inputPlist = "$rcptPkg/Contents/Info.plist";      
	
			print "INSTALLCHECK::main> Looking for $inputPlist\n";
	
			if (-f $inputPlist)
			{
				$vers = GetPListEntry($inputPlist, "CFBundleShortVersionString");
				print "INSTALLCHECK::main> found version $vers\n";
				$result = VersionCompare($vers, $REQUIRED_VERSION);         
				print "INSTALLCHECK::main> result: $result\n";
	
				if ($result ge "0")
				{
					# We've found an install with the correct required version
					print "INSTALLCHECK::main> Suitable previous version ($REQUIRED_VERSION) found!\n";
					return 0;
				}
			} 
		}
	
		# If we get here we didn't find a receipt file so use the pkgutil tool to
		# check for corresponding bundle ids. Note this really only works under OS X 10.6 and later
		foreach $bundle (@REQUIRED_BUNDLE_IDS)
		{
			print "INSTALLCHECK::main> Looking for bundle ID $bundle\n";
	
			if (BundleIsInstalled($bundle))
			{
				print "INSTALLCHECK::main> Bundle ID $bundle is installed\n";
	
				$vers = VersionForBundle($bundle);
				print "INSTALLCHECK::main> found version $vers\n";
				$result = VersionCompare($vers, $REQUIRED_VERSION);         
				print "INSTALLCHECK::main> result: $result\n";
	
				if ($result ge "0")
				{
					# We've found an install with the correct required version
					print "INSTALLCHECK::main> Suitable previous version ($REQUIRED_VERSION) found!\n";
					return 0;
				}
			} 
		}
	
		if($SUITE_INSTALL == 1)
		{
			# display a message (#16 in InstallationCheck.strings) and cancel install
			print "INSTALLCHECK::main> Suitable previous version ($REQUIRED_VERSION) NOT found!\n";
			return (3 << 5) + 16;
		}
		else
		{
			# display a message (#19 in InstallationCheck.strings) and cancel install
			print "INSTALLCHECK::main> Suitable previous version ($REQUIRED_VERSION) NOT found!\n";
			return (3 << 5) + 19;
		}
	}

#photoshop not required anymore
#	my @pluginDirs = FindUniquePSPluginDirs();
#	if (scalar(@pluginDirs) == 0)
#	{
#		print "INSTALLCHECK::main> No Photoshop installations found, prompt user\n";
#		return (3 << 5) + 18;
#	}
#	print "INSTALLCHECK::main> Suitable version of Photoshop found!\n";
	
	my $productsInstalled = FindProductInstalls();
	if ($productsInstalled == 1)
	{
		if($FREE_INSTALL == 1)
		{
			print "INSTALLCHECK::main> Other products found while trying to install free Version, prompt user\n";
			return (3 << 5) + 20;
		}
		else
		{
			print "INSTALLCHECK::main> Suite found while trying to install standalone Version, prompt user\n";
			return (3 << 5) + 21;
		}
	}

	#success
	return 0;
}


